const vm = new Vue({
    el: '#main',
    data: {
        authCodeText: '获取验证码',
        newAuthCodeText: '获取验证码',
        tel: '',
        newTel: '',
        code: '',
        newCode: '',
    },
    methods: {
        countDown,
    },
});

function apiready() {
    main();
}

function main() {
    fxj.fixLayout();
    fxj.api.getOldPhone().then(
        ([data, ok]) => {
            if (ok) {
                vm.tel = data;
            }
            else {
                toast(data);
            }
        },
    );
}


async function countDown(type) {
    if (type !== 'old' && vm.newTel === '') {
        mui.toast('请输入手机号码');
        return;
    }
    if (type !== 'old' && !fxj.verifyIpt(vm.newTel, 'tel')) {
        mui.toast('请输入正确格式的手机号码');
        return;
    }
    let tel = type === 'old' ? vm.tel : vm.newTel;
    let s = 60;
    const down = function () {
        setTimeout(() => {
            s--;
            if (type === 'old') {
                vm.authCodeText = `${s}s后重发`;
            }
            else {
                vm.newAuthCodeText = `${s}s后重发`;
            }
            if (s > 0) {
                down();
            }
            else {
                if (type === 'old') {
                    vm.authCodeText = '获取验证码';
                }
                else {
                    vm.newAuthCodeText = '获取验证码';
                }
            }
        }, 1000);
    };
    down();
    fxj.api.getAuthCode(tel).then(
        result => {
            if (!result) {
                s = 0;
            }
        },
    );
}

function submit() {
    ajax({
        type: 'POST',
        url: 'home/userCenter/modifyPhone',
        data: {
            oldPhone: vm.tel,
            oldCode: vm.code,
            newPhone: vm.newTel,
            newCode: vm.newCode,
        },
    }).then(
        result => {
            mui.toast(result.msg);
            if (result.err === 0) {
                fxj.closeWin();
            }
        },
    );
}
